package com.wufun.overseas.adapters;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.baselibrary.download.CommonDownloadTask;
import com.wufun.baselibrary.download.UtilsMy;
import com.wufun.baselibrary.impl.AdapterInterface;
import com.wufun.overseas.R;
import com.wufun.overseas.common.TabTypeInterface;
import com.wufun.overseas.models.db.DownLoadModel;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by tangbl on 2017/12/2 0002.
 */

public class PlayMainItemAdapter extends BaseAdapter {
    private Context context;
    private List<DownLoadModel> playDataList;
    private AdapterInterface mAdapterInterface;
    private LayoutInflater layoutInflater;
    private ViewHolder holder;
    private Uri uri;
    private PlayAdapterInterface mPlayAdapterInterface;

    public PlayMainItemAdapter(Context context, List<DownLoadModel> playDataList
            , AdapterInterface mAdapterInterface, PlayAdapterInterface mPlayAdapterInterface) {
        this.context = context;
        this.playDataList = playDataList;
        this.mAdapterInterface = mAdapterInterface;
        this.mPlayAdapterInterface = mPlayAdapterInterface;
        layoutInflater = LayoutInflater.from(context);
        uri = Uri.parse("res:///" + R.mipmap.localfile);
    }

    @Override
    public int getCount() {
        return playDataList.size();
    }

    @Override
    public DownLoadModel getItem(int position) {
        return playDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_download_view, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final DownLoadModel temp = playDataList.get(position);
        holder.downNameTx.setText(temp.getGameName());
        if (temp.getType() == TabTypeInterface.TYPE_LOCAL) {
            holder.downImgIv.setImageURI(uri);
        } else {
            holder.downImgIv.setImageURI(temp.getGameImg());
        }

        switch (temp.getStatus()) {
            case CommonDownloadTask.STATUS_ERROR:
                holder.btnOp.setText(context.getResources().getString(R.string.download_status_re_str));
                holder.btnOp.setBackgroundResource(R.drawable.recom_orange_butn);
                holder.btnOp.setTextColor(context.getResources().getColor(R.color.app_main_color));
                holder.downProgressBar.setVisibility(View.GONE);
                holder.appSize.setVisibility(View.GONE);
                holder.lodingInfo.setVisibility(View.GONE);
                holder.appTotalSize.setVisibility(View.VISIBLE);
                holder.lodingFailInfo.setVisibility(View.VISIBLE);
                break;
            case CommonDownloadTask.STATUS_STOP:
                holder.btnOp.setText(context.getResources().getString(R.string.download_status_goon_str));
                holder.lodingInfo.setText(context.getResources().getString(R.string.download_status_stop_str));
                holder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                holder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                holder.downProgressBar.setVisibility(View.VISIBLE);
                holder.appSize.setVisibility(View.VISIBLE);
                holder.lodingInfo.setVisibility(View.VISIBLE);
                holder.appTotalSize.setVisibility(View.GONE);
                holder.lodingFailInfo.setVisibility(View.GONE);
                break;
            case CommonDownloadTask.STATUS_START:
            case CommonDownloadTask.STATUS_PROGRESS:
                holder.appSize.setText(UtilsMy.FormatFileSize(temp.getProgress() * temp.getTotalSize() / 100)
                        + "/" + UtilsMy.FormatFileSize(temp.getTotalSize()));
                holder.btnOp.setText(context.getResources().getString(R.string.download_status_stop_str));
                holder.lodingInfo.setText(temp.getSpeed() == null ? "0KB" : temp.getSpeed() + "/s");
                holder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                holder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                holder.downProgressBar.setVisibility(View.VISIBLE);
                holder.appSize.setVisibility(View.VISIBLE);
                holder.lodingInfo.setVisibility(View.VISIBLE);
                holder.appTotalSize.setVisibility(View.GONE);
                holder.lodingFailInfo.setVisibility(View.GONE);
                break;
            case CommonDownloadTask.STATUS_FINISH:
                holder.downProgressBar.setVisibility(View.GONE);
                holder.appSize.setVisibility(View.GONE);
                holder.lodingInfo.setVisibility(View.GONE);
                holder.appTotalSize.setVisibility(View.VISIBLE);
                holder.lodingFailInfo.setVisibility(View.GONE);
                holder.btnOp.setText(context.getResources().getString(R.string.download_status_open_str));
                holder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                holder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                holder.appTotalSize.setText(UtilsMy.FormatFileSize(temp.getTotalSize()));
                break;
            default:
                holder.btnOp.setText(context.getResources().getString(R.string.download_status_start_str));
                holder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                holder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                holder.downProgressBar.setVisibility(View.GONE);
                holder.appSize.setVisibility(View.GONE);
                holder.lodingInfo.setVisibility(View.GONE);
                holder.appTotalSize.setVisibility(View.VISIBLE);
                holder.lodingFailInfo.setVisibility(View.GONE);
                holder.appTotalSize.setText(UtilsMy.FormatFileSize(temp.getTotalSize()));
                break;
        }
        if (holder.downProgressBar.getVisibility() == View.VISIBLE)
            holder.downProgressBar.setProgress(temp.getProgress());

        holder.rLayoutRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapterInterface.onAdapterItemClick(v, position);
            }
        });
        holder.btnOp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapterInterface.onAdapterItemClick(v, position);
            }
        });

        if (mPlayAdapterInterface!=null) {
            holder.itemLl.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mPlayAdapterInterface.onAdapterItemLongChildClick(v, position);
                    return true;
                }
            });
        }

        return convertView;
    }

    public interface PlayAdapterInterface {

        void onAdapterItemLongChildClick(View view, int position);
    }

    static class ViewHolder {
        @Bind(R.id.down_img_iv)
        SimpleDraweeView downImgIv;
        @Bind(R.id.down_name_tx)
        TextView downNameTx;
        @Bind(R.id.appTotalSize)
        TextView appTotalSize;
        @Bind(R.id.loding_fail_info)
        TextView lodingFailInfo;
        @Bind(R.id.appSize)
        TextView appSize;
        @Bind(R.id.loding_info)
        TextView lodingInfo;
        @Bind(R.id.down_progressBar)
        ProgressBar downProgressBar;
        @Bind(R.id.btnOp)
        TextView btnOp;
        @Bind(R.id.rLayoutRight)
        RelativeLayout rLayoutRight;
        @Bind(R.id.item_ll)
        LinearLayout itemLl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
